﻿function RemoveEmptySkills(skills) {
    var htmlSkills = "";
    var emp = "";
    for (var i = 0; i < skills.length; i++) {
        if (skills[i].length == 0 && i < 3)
            emp += "<br \>"
        if (skills[i].length != 0) {
            htmlSkills += skills[i];
            if (i < 3)
                htmlSkills += "<br \>";
        }
    }

    return htmlSkills + emp;
}
function GetMonthYear(totalExp) {

    //alert(totalExp);

    var profExperience;

    if (totalExp == 0) {
        profExperience = "below 1 month";
    }
    else if (totalExp < 12 && totalExp > 1) {
        profExperience = totalExp + " months";
    }
    else if (totalExp > 12) {
        var years = Math.floor(totalExp / 12);
        var months = totalExp % 12;
        var str;

        if (years != "") {
            str = years + " year(s) "
        }
        if (months != "") {
            str += months + " months(s)"
        }

        profExperience = str;
    }

    return profExperience;
}

function PrintGrid(data, pageIndex) {
    //call autocomplete of title datasource,with existing one
    if (data.d == "")
        $("#datarows").empty();
    else {
        var xmlDoc = $.parseXML(data.d);
        var xml = $(xmlDoc);
        var customers = xml.find("GridData");
        var pager = xml.find("PagerData");
        var countRecords = pager.find("RecordCount").text();
        if (pager.find("RecordCount").text() != "0") {
            $("div[id='datarowsGrid']").remove();
            var table = $("#datarows");
            var i = 0;
            $.each(customers, function () {
                var customer = $(this);
                i++;

                var exp1 = parseInt($(this).find("companyExp1").text());
                var exp2 = parseInt($(this).find("companyExp2").text());
                var exp3 = parseInt($(this).find("companyExp3").text());

                var skills = [$(this).find("SkillOne").text(), $(this).find("SkillTwo").text(), $(this).find("SkillThree").text(), $(this).find("SkillFour").text()];

                var desiredSalary;
                if ($(this).find("ExpectedSalaryRange").text() != "") {
                    desiredSalary = $(this).find("ExpectedSalaryRange").text();
                } else {
                    desiredSalary = "N/A";
                }
                //alert(desiredSalary);



                var totalExp = (isNaN(exp1) ? 0 : exp1) + (isNaN(exp2) ? 0 : exp2) + (isNaN(exp3) ? 0 : exp3);
                //alert( "cvLastUpdated "+$(this).find("cvLastUpdated").text());
                //alert(totalExp);

                var profExperience = GetMonthYear(totalExp);



                var htmlRow = "<div id=\"datarowsGrid\" class=\"row search-cv\">"
                               + "<div class=\"row\">"
                               + "<h3>" + $(this).find("FirstName").text() + "," + $(this).find("LastName").text() + "</h3>"
                               + "<div class=\"col-md-6\">"
                               + "<p>"
                               + $(this).find("Degree").text() + ", " + $(this).find("InstitutionName").text()
                               + "<br />"
                               + $(this).find("Title").text()
                               + "<br />" + $(this).find("CompanyName").text() + "</p>"
                               + "</div>"
                               + "<div class=\"col-md-6 pull-right\">"
                               + "<div class=\"col-md-4\">"
                               + "<span><strong>CV Update</strong></span> <br \>"
                               + "<span><strong>Experiences</strong></span><br \>"
                               + "<span><strong>Desired Salary</strong></span>"
                               + "</div>"
                               + "<div class=\"col-md-8\">: " + $(this).find("cvLastUpdated").text() + "<br />"
                               + ": " + profExperience + "<br />"
                               + ": " + desiredSalary
                               + "</div>"
                               + "</div>"
                               + "</div>"
                               + "<div class=\"clearfix\">&nbsp;</div>"
                               + "<div class=\"row skill-in-cv\">"
                               + "<div class=\"col-md-4\">"
                               + "<span><strong>Top Skill(s)</strong></span><br>"
                               + RemoveEmptySkills(skills)
                               //+ $(this).find("SkillOne").text() + "<br />"
                               //+ $(this).find("SkillTwo").text() + "<br />"
                               //+ $(this).find("SkillThree").text() + "<br />"
                               //+ $(this).find("SkillFour").text()
                               + "</div>"
                               + "<div class=\"col-md-4\">"
                               + "<span><strong>Company</strong></span><br /> "
                               + $(this).find("company1").text() + "<br />"
                               + $(this).find("company2").text() + "<br />"
                               + $(this).find("company3").text()
                               + "</div>"
                               + "<div class=\"col-md-4\">"
                               + "<span><strong>Experience</strong></span><br>"
                               + GetMonthYear($(this).find("companyExp1").text()) + "<br />"
                               + GetMonthYear($(this).find("companyExp2").text()) + "<br />"
                               + GetMonthYear($(this).find("companyExp3").text())
                                + "</div>"
                                + "</div>"
                                + "<div class=\"row pull-right search-cs-details-btn\">"
                                + "<a target=\"_blank\" class=\"link-btn btn btn-primary orange-gradiant float-shadow\" href='SearchCvDetails.aspx?id=" + $(this).find("JobSeekerID").text() + "'>"
                                + "View Details</a>"
                                + "</div></div>";

                $("#datarows").prepend(htmlRow);

                var pageSize2 = $('#ctl00_MainContent_ddlPageSize option:selected').val();

                if ($.isNumeric(pageSize2)) {

                    var records = "<nav>"
                     + "<ul class=\"pager\">"
                     + "<li><a href=\"#\">" + (pageSize2 * (pageIndex - 1) + 1) + " - " + (pageSize2 * pageIndex > countRecords ? countRecords : pageSize2 * pageIndex)
                     + " of About " + countRecords + "</a></li>"
                     + "</ul>"
                     + "</nav>";
                    $("#PageRecords").empty();
                    $("#PageRecords").append(records);

                }
                var pager = xml.find("PagerData");
                $(".Pager").ASPSnippets_Pager({
                    ActiveCssClass: "current",
                    PagerCssClass: "pager",
                    PageIndex: pageIndex,
                    PageSize: $('#ctl00_MainContent_ddlPageSize option:selected').val(),
                    RecordCount: parseInt(pager.find("RecordCount").text())
                });
            });
        }
        else {
            window.scrollTo(0, 0);
            $("#NoRecordsMessage").css({ display: "block" });
            $("#NoRecordsMessage").fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000).fadeIn(1000).fadeOut(1000);
        }
    }
}